package tk.xboot.ws;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import okio.ByteString;

import java.util.HashMap;
import java.util.Map;

public class OkHttpWsCli {

    public static void main(String[] args) {
        startRequest();
    }


    private static final int NORMAL_CLOSURE_STATUS = 1000;
    private static OkHttpClient sClient;
    private static WebSocket sWebSocket;

    public static synchronized void startRequest() {
        if (sClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.hostnameVerifier(new NullHostnameVerifier());
            sClient = builder.build();
        }
        if (sWebSocket == null) {
            //Request request = new Request.Builder().url("ws://127.0.0.1:8080/ws/33").build();
            Request request = new Request.Builder().url("wss://47.75.1.45:443/realtime").build();
            EchoWebSocketListener listener = new EchoWebSocketListener();
            sWebSocket = sClient.newWebSocket(request, listener);
        }
    }
    private static void sendMessage(WebSocket webSocket) {
        Map map = new HashMap<String,String>();
        map.put("cmd", "sub");
        map.put("type","card");
        map.put("set", "BTCFuturesContract,ETHFuturesContract,EOSFuturesContract,LTCFuturesContract,BCHFuturesContract,XRPFutures,ContractFinancingRate,ETHEyeOfTheSky,BTCPerpetualContract,ETHPerpetualContract,ExchangeCapital,USDTIndex,TokenEyeOfTheSky,PlatformBurstStatistics,24hPlatformBurstStatistics,RiseList,FallList,IndexAssistant,ShortTerm,RiseAndFallDistribution,firmOfferForLiyiqi,coinVolume,bigVip,selfSelectionCoins");
        String msg = "";
        try {
            msg = new ObjectMapper().writeValueAsString(map);
        } catch (Exception e) {

        }
        webSocket.send(msg);
        //webSocket.send(ByteString.decodeHex("deadbeef"));
    }
    public static void sendMessage() {
        WebSocket webSocket;
        synchronized (OkHttpWsCli.class) {
            webSocket = sWebSocket;
        }
        if (webSocket != null) {
            sendMessage(webSocket);
        }
    }
    public static synchronized void closeWebSocket() {
        if (sWebSocket != null) {
            sWebSocket.close(NORMAL_CLOSURE_STATUS, "Goodbye!");
            sWebSocket = null;
        }
    }
    public static synchronized void destroy() {
        if (sClient != null) {
            sClient.dispatcher().executorService().shutdown();
            sClient = null;
        }
    }
    private static void resetWebSocket() {
        synchronized (OkHttpWsCli.class) {
            sWebSocket = null;
        }
    }
    public static class EchoWebSocketListener extends WebSocketListener {
        private static final String TAG = "EchoWebSocketListener";
        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            sendMessage(webSocket);
        }
        @Override
        public void onMessage(WebSocket webSocket, String text) {
            System.out.println(text);
            //Log.i(TAG, "Receiving: " + text);
        }
        @Override
        public void onMessage(WebSocket webSocket, ByteString bytes) {
            //Log.i(TAG, "Receiving: " + bytes.hex());
        }
        @Override
        public void onClosing(WebSocket webSocket, int code, String reason) {
            webSocket.close(NORMAL_CLOSURE_STATUS, null);
            //Log.i(TAG, "Closing: " + code + " " + reason);
            resetWebSocket();
        }
        @Override
        public void onClosed(WebSocket webSocket, int code, String reason) {
            //Log.i(TAG, "Closed: " + code + " " + reason);
        }
        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            t.printStackTrace();
            resetWebSocket();
        }
    }
}
